package cn.edu.hit.filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.xml.internal.ws.util.StringUtils;

/**
 * Servlet Filter implementation class MethodFilter
 */
public class MethodFilter implements Filter {

	/**
	 * Default constructor.
	 */
	public MethodFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("**********destory***********");
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String method = req.getMethod();
		String server = req.getServerName();
		Enumeration<String> referer = req.getHeaders("Referer");
		String reqURI = req.getRequestURI();

		if (req.getHeader("Referer")!=null && !check(referer, server)) {
			System.out.println("****跨域提交****");
			res.sendRedirect(req.getContextPath()+"/error.html");
			return;
		}
		if (reqURI.indexOf("AjaxAction") > 0 && "GET".equalsIgnoreCase(method)) {
			System.out.println("****不合法的GET请求****");
			res.sendRedirect(req.getContextPath()+"/error.html");
			//res.setStatus(404);  
			return; 
		}
		
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	@SuppressWarnings("null")
	public Boolean check(Enumeration<String> headerValues, String servername_str) {
		String Referer = "";
		while (headerValues.hasMoreElements()) {
			Referer = (String) headerValues.nextElement();
			// 判断是否存在请求页面
			if (Referer == null || Referer.length() >= 1){
				// 判断请求页面和getRequestURI是否相同
				if (servername_str != null || servername_str.length() > 0) {
					int index = 0;
					if (Referer.indexOf("https://") == 0) {
						index = 8;
					} else if (Referer.indexOf("http://") == 0) {
						index = 7;
					}
					if (Referer.length() - index >= servername_str.length()){ // 比较字符串（主机名称）是否相同
						String referer_str = Referer.substring(index, index
								+ servername_str.length());
						if (servername_str.equalsIgnoreCase(referer_str))
							return true;
					}
				} 
			}			
		}
		return false;
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("**********init***********");
	}

}
